const request = require('request')
const qs = require('querystring')
const fs = require('fs')


module.exports=(accessToken,img,nodeRes)=>{
    let url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general?access_token=" + accessToken
    let headers = {
        "Content-Type": "application/x-www-form-urlencoded"
    }
    let imageBase64 = fs.readFileSync(img).toString("base64")
    let body = {
        "image": imageBase64
    }
    request({
        url: url,
        method: "POST",
        headers: headers,
        body: qs.stringify(body)
    }, (err, res, body) => {
        let result = []
        let newBody = JSON.parse(body)
        for(let i=0;i<JSON.parse(body).result_num;i++){
            result[i]={name:newBody.result[i].root,score:newBody.result[i].score}
        }
        nodeRes.send({ 
            data: img.split('/')[(img.split('/').length)-1],
            result:result
        })
    })

}
